package edu.lingnan.librarymanage.config.web;

import edu.lingnan.librarymanage.core.constant.LibraryManageConstant;
import edu.lingnan.librarymanage.core.vo.LoginUserVO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cql
 */
public class MyHandlerInterceptor implements HandlerInterceptor {

    /**
     * 拦截器
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 后台管理系统必须要登录后才能访问,并且登录账户是管理员
         */
        LoginUserVO user = (LoginUserVO) request.getSession().getAttribute(LibraryManageConstant.LOGIN_USER);
        if (user == null || !user.isAdmin()) {
            response.sendRedirect(request.getContextPath() + "/login.html");
            return false;
        }
        return true;
    }


}
